// pages/sign/index.js
import { randomString } from '../../utils/index'
import request from '../../utils/request'
Page({

  /**
   * 页面的初始数据
   */
  data: {
    workType: ['文字', '图片', '视频'],
    signForm: {
      // competition_name: '海南医学院植树大赛',
      competition_type: '团队赛',
      competition_team_limit: '4人',
      // competition_is_work: '作品赛',
      // competition_work_type: '文字',
      student_name: '杨淳睿',
      university: '海南医学院',
      college: '生物医学信息与工程学院',
      class_name: '18信管2班',
      date: '2022-05-17',
      student_id: '181201223',
      phone_number: 17803685117,
      isCaptain: false,
      team_name: '猪拱白菜队',
      team_code: '',
      teacher_name: '',
      teacher_id: ''
    },
    competition: null
  },

  fetchInputValue(event) {
    const { signForm } = this.data
    const { key } = event.currentTarget.dataset
    const value = event.detail.detail.value
    signForm[key] = value
    console.log(signForm)
    this.setData({ signForm })
  },

  switchChange({ detail }) {
    const { value } = detail
    const { signForm } = this.data
    signForm.isCaptain = value
    // 如果选中为队长且队伍码为空时，填入随机码
    if (value && !signForm.team_code) {
      signForm.team_code = randomString(5, true)
    } else {
      // 否则清空
      signForm.team_code = ''
    }
    this.setData({ signForm })
  },

  doSign() {
    const { signForm, competition } = this.data
    if (!signForm.team_code) {
      wx.showToast({ title: '请填入队伍码', mask: true, icon: 'error' })
      return void 0
    }
    wx.showLoading({ mask: true, title: '上传报名信息' })
    request.post('/sign', { ...signForm, competition_id: competition._id }).then(result => {
      wx.hideLoading()
      wx.navigateBack()
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    const _this = this
    const eventChannel = this.getOpenerEventChannel()
    eventChannel.on('deliverCompetitionInfo', function (data) {
      _this.setData({
        competition: data.data
      })
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})